'use strict';

module.exports = app => {
  const { INTEGER, DATE, BOOLEAN } = app.Sequelize;

  const Follow = app.model.define('follow', {
    id: {
      type: INTEGER,
      primaryKey: true,
      autoIncrement: true,
    },
    follower_id: {
      type: INTEGER,
      allowNull: false,
      comment: '关注者ID',
    },
    following_id: {
      type: INTEGER,
      allowNull: false,
      comment: '被关注者ID',
    },
    is_active: {
      type: BOOLEAN,
      defaultValue: true,
      comment: '是否激活',
    },
    created_at: {
      type: DATE,
      allowNull: false,
      defaultValue: app.Sequelize.NOW,
    },
    updated_at: {
      type: DATE,
      allowNull: false,
      defaultValue: app.Sequelize.NOW,
    },
  }, {
    tableName: 'follows',
    timestamps: true,
    paranoid: false,  // 禁用软删除
    underscored: true,
    createdAt: 'created_at',
    updatedAt: 'updated_at',
    comment: '关注关系表',
    indexes: [
      {
        unique: true,
        fields: ['follower_id', 'following_id'],
      },
    ],
  });

  // 关联关系
  Follow.associate = function() {
    // 关注者
    app.model.Follow.belongsTo(app.model.User, {
      foreignKey: 'follower_id',
      as: 'follower',
    });

    // 被关注者
    app.model.Follow.belongsTo(app.model.User, {
      foreignKey: 'following_id',
      as: 'following',
    });
  };

  return Follow;
};
